<?php

namespace app\ports\controller;

use think\Config;

/**
 * 交易
 * Class
 * @package
 */
class Trans
{

	use \app\common\traits\ToolTrait;

	/**
	 * 对象实例
	 * @var object
	 */
	protected static $instance;

	/**
	 * 获取实例对象
	 * @return Binds|object
	 */
	public static function instance()
	{
		if (is_null(self::$instance)) {
			self::$instance = new static();
		}
		return self::$instance;
	}

	public function pro_real_time_trans($data)
	{
		if (!empty($data)) {
			try {
				$option = [];
				$option = $this->_require_fields($data);
				if (!empty($option)) {
					$option["cards"] = $this->_cards_tp($option["cards"]);
					$binds = \app\ports\controller\publics\Binds::instance()->_sn_binds_info($option["sn"]);
					$proCheck = \app\ports\controller\publics\Template::instance()->_check_calc_product_template($binds["proid"], $option["cards"], $option["trans"], $option["route"]);
					if ($proCheck) {
						// 月存储
						$this->_store_process($binds, $option);
						// 按天存储
						$this->_days_store_process($binds, $option);
					}
				}
			} catch (\Exception $e) {
				$this->logs("./portsLogs/bonus", "bonus_trans" . date("Y-m") . ".txt", [$e->getMessage()]);
			}
		}
	}

	/**
	 * 存储交易-处理(按天存储)
	 */
	protected function _days_store_process($binds, $option)
	{
		$times =  date("Y", $option["trades"]);
		$table = "bonus_calc_trans_days_{$times}";
		$storeTable = \app\ports\controller\publics\Tables::instance()->bonus_trans_days_store_tables($table, $times);
		if ($storeTable) {
			$option["nums"] = 1; // 笔数 
			$option["times"] = strtotime(date("Y-m-d", $option["trades"]));
			$option = array_merge($option, $binds);
			\app\ports\controller\publics\DataSql::instance()->transSave($table, $option);
			// 实时计算 团队达标
			$this->real_person_stands($binds["agid"], $option["sn"]);
		}
	}

	/**
	 * 团队达标
	 */
	public function real_person_stands($agid, $sn)
	{
		\app\ports\controller\publics\Stands::instance()->handle($agid, $sn);
	}

	/**
	 * 存储交易-处理(月存储)
	 */
	protected function _store_process($binds, $option)
	{
		$times =  date("Y", $option["trades"]);
		$table = "bonus_calc_trans_{$times}";
		$storeTable = \app\ports\controller\publics\Tables::instance()->bonus_trans_store_tables($table, $times);
		if ($storeTable) {
			$option["nums"] = 1; // 笔数 
			$option["times"] = strtotime(date("Y-m", $option["trades"]));
			$option = array_merge($option, $binds);
			\app\ports\controller\publics\DataSql::instance()->transSave($table, $option);
		}
	}

	/**
	 * 所需字段-格式
	 * @return array
	 */
	public function _require_fields($data)
	{
		$option = [];
		$arrs = [
			"sn"					=> "sn",
			"transaction_amount" 	=> "transAmt",
			"transaction_type"		=> "trans",
			"transaction_rate"		=> "rates",
			"card_type"				=> "cards",
			"gather_type" 			=> "route",
			"trading_time"			=> "trades"
		];
		if (!empty($data)) {
			foreach ($arrs as $key => $val) {
				$option[$val] = $data[$key];
			}
		}
		return $option;
	}

	/**
	 * 卡类型
	 */
	public function _cards_tp($tp)
	{
		return Config::get("cards." . $tp)['id'];
	}
}
